package utils;

import service.SystemCommandService;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.List;

/**
 * 执行cmd命令工具类
 *
 * @author dufei
 */
@Component
public class CMDUtil {

    private SystemCommandService service = null;

    @PostConstruct
    public void init() {
        List<Class> classList = ClassUtil.getAllClassByInterface(SystemCommandService.class);
        for (Class classItem : classList) {
            try {
                SystemCommandService impl = (SystemCommandService) classItem.newInstance();
                if (OSInfo.getosname().equals(impl.getType())) {
                    service = impl;
                    break;
                }
            } catch (Exception e) {
                throw new RuntimeException("初始化系统命令失败", e);
            }
        }
    }

    public Process excuteCMDCommand(String cmdCommand) {
        if (service == null){
            throw new RuntimeException("该系统命令未实现");
        }
        return service.excuteCMDCommand(cmdCommand);
    }

    public void killProcessTree(Process process, String rtspUrl) {
        if (service == null){
            throw new RuntimeException("该系统命令未实现");
        }
        service.killProcessTree(process, rtspUrl);
    }

}
